/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.persistence;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author Julien
 */
@Entity
@Table(name="INVOICE")
@NamedQueries({
        @NamedQuery(name="Invoice.findByOrder", query="SELECT i FROM Invoice i WHERE i.orders = :order"),
        @NamedQuery(name="Invoice.findByBook", query="SELECT i FROM Invoice i WHERE i.book = :book"),
        @NamedQuery(name="Invoice.findMostSoldBook", query="SELECT i.book, SUM(i.quantity) AS quantityBook FROM Invoice i WHERE i.book.available = true GROUP BY i.book ORDER BY quantityBook DESC")})
public class Invoice implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @JoinColumn(name = "Id_Book")
    @ManyToOne
    private Book book;
    
    @Id
    @JoinColumn(name = "Id_Order")
    @ManyToOne
    private Order orders;
    
    @Column(name="Quantity")
    private int quantity = 1;

/* -----------------------------------------------------------------------------
 * ---------------------------GETTERS AND SETTERS-------------------------------
 * ---------------------------------------------------------------------------*/

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        if(quantity <= 0){
            this.quantity = 1;
        } else {
            this.quantity = quantity;
        }
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public Order getOrders() {
        return orders;
    }

    public void setOrders(Order orders) {
        this.orders = orders;
    }
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (book != null ? book.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Invoice)) {
            return false;
        }
        Invoice other = (Invoice) object;
        if ((this.book == null && other.book != null) || (this.book != null && !this.book.equals(other.book))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ejb.persistence.Invoice[ id=" + book + " ]";
    }
    
}
